package com.softserveinc.interactive_board.dao;

import java.util.List;

import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import com.softserveinc.interactive_board.domain.Presentation;
import com.softserveinc.interactive_board.domain.PresentationStatus;
import com.softserveinc.interactive_board.domain.User;
import com.softserveinc.interactive_board.domain.UserType;

@Repository
public class PresentationDAOImpl implements PresentationDAO {

	public boolean addPresentation(Presentation presentation) {
		Session session = this.sessionFactory.getCurrentSession();
		List<User> list = session
				.createQuery(
						"from User u where u.id = :user_id and u.userType.id = :type_id")
				.setString("user_id", presentation.getUser().getId().toString())
				.setString("type_id", String.valueOf(UserType.ID_PRESENTOR))
				.list();
		if (list.isEmpty())
			return false;
		session.flush();
		session.clear();
		sessionFactory.getCurrentSession().save(presentation);
		return true;
	}

	public List<PresentationStatus> getPresentationStatuses() {
		return sessionFactory.getCurrentSession()
				.createQuery("from PresentationStatus p order by p.id").list();
	}

	public List<Presentation> getPresentations() {
		return sessionFactory.getCurrentSession()
				.createQuery("from Presentation p order by p.id").list();
	}

	public List<Presentation> getPresentationsByStatusId(int id) {
		return this.sessionFactory.getCurrentSession()
				.createQuery("from Presentation p where p.status.id = :id")
				.setString("id", String.valueOf(id)).list();
	}

	public List<Presentation> getPresentationsByPresentorId(int id) {
		return this.sessionFactory.getCurrentSession()
				.createQuery("from Presentation p where p.user.id = :id")
				.setString("id", String.valueOf(id)).list();
	}

	public Presentation getPresentation(Presentation pres) {
		List<Presentation> list = this.sessionFactory.getCurrentSession()
				.createQuery("from Presentation p where p.id = :id")
				.setString("id", pres.getId().toString()).list();
		if (list.isEmpty())
			return null;
		return list.get(0);
	}

	public boolean updatePresentation(Presentation pres) {
		System.out.println("update");
		Session session = this.sessionFactory.getCurrentSession();
		List<Presentation> presList = session
				.createQuery("from Presentation p where p.id = :id")
				.setString("id", pres.getId().toString()).list();
		if (presList.isEmpty())
			return false;
		session.clear();
		System.out.println("find pres");
		List<User> usList = session
				.createQuery(
						"from User u where u.id = :user_id and u.userType.id = :user_type_id")
				.setString("user_id", pres.getUser().getId().toString())
				.setString("user_type_id",
						String.valueOf(UserType.ID_PRESENTOR)).list();
		if (usList.isEmpty())
			return false;
		session.clear();
		session.update(pres);
		return true;
	}

	public void deletePresentation(Presentation presentation) {
		this.sessionFactory.getCurrentSession().delete(presentation);
	}

	public void deletePresentation(Long presId) {
		this.deletePresentation(new Presentation(presId));
	}

	public void deletePresentations(List<Presentation> list) {
		System.out.println("Here");
		for (Presentation p : list) {
			System.out.println(p.getId());
			this.deletePresentation(p);
		}
	}

	private SessionFactory sessionFactory;

	@Autowired
	@Qualifier("sessionFactory")
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

}
